﻿using AuthCenter_HuangJingtao.Domain;
using AuthCenter_HuangJingtao.ErrorCode;
using AuthCenter_HuangJingtao.Infrastructure.Imterfaces;
using AuthCenter_HuangJingtao.Read.Api.Application.Command;
using AuthCenter_HuangJingtao.Read.Api.Application.Command.MenuaLL;
using MediatR;

namespace AuthCenter_HuangJingtao.Read.Api.Application.CommandHandler.MenuaLL
{
    public class SeTreeCommandHandler : IRequestHandler<SeTreeCommand, ApiResult<List<ApiTree>>>
    {
        IBaseReponsitory<Roles> Roles;

        public SeTreeCommandHandler(IBaseReponsitory<Roles> Roles)
        {
            this.Roles = Roles;
        }

        public async Task<ApiResult<List<ApiTree>>> Handle(SeTreeCommand request, CancellationToken cancellationToken)
        {

            var list = Roles.GetQuery().ToList();

            List<ApiTree> trees = new List<ApiTree>();
            foreach (var item in list)
            {
                ApiTree tree = new ApiTree();
                tree.id = item.Role_Id;
                tree.label = item.Role_Name;
                trees.Add(tree);
            }

            return new ApiResult<List<ApiTree>>
            {
                Code = ApiEnum.Success,
                Message = "查询成功",
                Data = trees
            };
        }
    }
}
